package com.samples.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

/**
 *
 * @author Bora SAYINER
 * @since Jan 24, 2015 5:30:07 PM
 * @version 1.0
 */
@WebServlet(urlPatterns = { "/ComboBoxServlet.do" })
public class ComboBoxServlet extends HttpServlet {

	private final Map<Integer, String> istanbulIlceler;
	private final Map<Integer, String> ankaraIlceler;

	public ComboBoxServlet() {
		istanbulIlceler = new HashMap<Integer, String>();
		istanbulIlceler.put(1, "Kadiköy");
		istanbulIlceler.put(2, "Levent");
		istanbulIlceler.put(3, "Kayisdayi");

		ankaraIlceler = new HashMap<Integer, String>();
		ankaraIlceler.put(1, "Cankaya");
		ankaraIlceler.put(2, "Mamah");
		ankaraIlceler.put(3, "Dikmen");
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int id = Integer.valueOf(req.getParameter("ilId"));
		Map<Integer, String> selectedIlceler = null;
		selectedIlceler = (id == 1) ? istanbulIlceler : ankaraIlceler;
		Gson gson = new Gson();
		String result = gson.toJson(selectedIlceler);
		resp.setContentType("text/json");
		resp.getWriter().print(result);
	}
}
